package com.jfreer.mybatis.expand.javamapper.elements;

import com.jfreer.mybatis.expand.IntrospectedTableMyBatis3Freework;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;

import java.util.Set;
import java.util.TreeSet;

/**
 * User: landy
 * Date: 14-6-28
 * Time: 下午6:43
 */
public class SelectBySelectiveWithPageMethodGenerator extends AbstractJavaMapperMethodGenerator {
    @Override
    public void addInterfaceElements(Interface interfaze) {
        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
        importedTypes.add(getPageListType());
        importedTypes.add(getPageBoundsType());

        FullyQualifiedJavaType parameterType;

        if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
            parameterType = new FullyQualifiedJavaType(introspectedTable
                    .getRecordWithBLOBsType());
        } else {
            parameterType = new FullyQualifiedJavaType(introspectedTable
                    .getBaseRecordType());
        }

        Method method = new Method();
        method.setVisibility(JavaVisibility.PUBLIC);

        FullyQualifiedJavaType returnType = getPageListType();
        FullyQualifiedJavaType listType;
        listType = new FullyQualifiedJavaType(
                introspectedTable.getBaseRecordType());

        importedTypes.add(listType);
        returnType.addTypeArgument(listType);
        method.setReturnType(returnType);
        method.setName(((IntrospectedTableMyBatis3Freework) introspectedTable).getSelectBySelectiveStatementId());
        method.addParameter(new Parameter(parameterType, "record")); //$NON-NLS-1$
        method.addParameter(new Parameter(getPageBoundsType(),"page"));

        context.getCommentGenerator().addGeneralMethodComment(method,
                introspectedTable);

        interfaze.addImportedTypes(importedTypes);
        interfaze.addMethod(method);
    }

    private FullyQualifiedJavaType getPageBoundsType() {
        return new FullyQualifiedJavaType("com.github.miemiedev.mybatis.paginator.domain.PageBounds");
    }

    private FullyQualifiedJavaType getPageListType() {
        return new FullyQualifiedJavaType("com.github.miemiedev.mybatis.paginator.domain.PageList");
    }
}
